/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   generator_io.h
 * Author: lpiccinelli
 *
 * Created on 12 agosto 2010, 22.15
 */

#ifndef GENERATOR_IO_H
#define	GENERATOR_IO_H

#include <string>
#include <vector>

#include "prj_definitions.h"
#include "pipeline/pipeline_io.h"
#include "configuration/configuration.h"

namespace NAMESPACE{

// ****** Generator interface definition ***************************************
class Generator{
public:
    typedef IOElement generated_t;

    virtual ~Generator(){}
    
    /**
     * Generate a new IOelement, which depends on the specific implementation
     *
     * @return the generated element
     */
    virtual IOElement* generate(void) = 0;
    virtual IOElement* generate(const IOKey& key) = 0;

    virtual Generator& set_key(const IOKey& k) = 0;
    virtual Generator& set_key(const std::string& ns, const std::string& key_name) = 0;
    virtual const IOKey& get_key() const = 0;

    virtual IOElement* last_generated() const = 0;

    virtual bool operator==(const Generator& g) const = 0;
    virtual bool operator!=(const Generator& g) const = 0;
};
// -----------------------------------------------------------------------------

// ****** AbstractGenerator definition *****************************************
class AbstractGenerator : public Generator{
public:
    const static memory_size_t DEFAULT_MAX_MEM_SIZE;
    static ConfigurationTemplate conf_tmplt;

protected:
    std::vector<IOElement*>* generated_buffer; ///< This is used to mantain an history of
                                               ///< generated IOElements.

    IOKey* key; ///< key assigned to generated elements
    
    bool is_a_copy; ///< states if the current instance is a copy of another instance
    memory_size_t max_buffer_memory;

    virtual void free_buffer(std::vector<IOElement*>* buffer);
    virtual void free_buffer();

    virtual void free_element(IOElement* el) = 0;
    virtual memory_size_t measure_memory() = 0;

public:
    virtual ~AbstractGenerator();
    AbstractGenerator();
    AbstractGenerator(const AbstractGenerator& ag);
    AbstractGenerator& operator=(const AbstractGenerator& ag);

    AbstractGenerator(const memory_size_t& _max_mem);
    AbstractGenerator(const IOKey& k, const memory_size_t& _max_mem = DEFAULT_MAX_MEM_SIZE);

    virtual Generator& set_key(const IOKey& k);
    virtual Generator& set_key(const std::string& ns, const std::string& key_name);
    Generator& set_max_buffer_mem(const memory_size_t& max_mem);
    virtual const IOKey& get_key() const;
    const memory_size_t& get_max_buffer_mem() const;
    const std::vector<IOElement*>& get_buffer() const;

    virtual IOElement* last_generated() const;

    virtual IOElement* generate(void);
    virtual IOElement* generate(const IOKey& key) = 0;

    virtual bool operator==(const Generator& g) const;
    virtual bool operator!=(const Generator& g) const;
};
// -----------------------------------------------------------------------------

REGISTER_PARSE_TYPE(Generator)
REGISTER_PARSE_TYPE(Generator*)

}

#endif	/* GENERATOR_IO_H */
